package com.kly.cashmall.module.home.home_tab.tab_view;

import android.content.Context;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

import com.kly.cashmall.AppApplication;
import com.kly.cashmall.utils.function.GlideUtils;
import com.kly.cashmall.utils.data.TextUtil;


/**
 * @author
 * @date 2017/12/22
 */
public class MenuImageView extends AppCompatImageView {

  private String selectedUrl;
  private String unselectedUrl;

  public MenuImageView(Context context) {
    super(context);
  }

  public MenuImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public MenuImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  /**
   * 设置选中图片和非选择推片
   */
  public void setSelectUrls(String selectedUrl, String unselectedUrl) {
    this.selectedUrl = selectedUrl;
    this.unselectedUrl = unselectedUrl;
    refreshMenuDrawableState();
  }

  @Override protected void dispatchSetSelected(boolean selected) {
    super.dispatchSetSelected(selected);
    refreshMenuDrawableState();
  }

  @Override protected void dispatchSetPressed(boolean pressed) {
    super.dispatchSetPressed(pressed);
    refreshMenuDrawableState();
  }

  private void refreshMenuDrawableState() {
    if (!TextUtil.isEmpty(selectedUrl) && !TextUtil.isEmpty(unselectedUrl)) {
      GlideUtils.loadImage(AppApplication.getInstance(), (isSelected() || isPressed()) ? selectedUrl : unselectedUrl,this);
    }
  }
}
